using System;
using System.Collections.Generic;

using GarageGames.Torque.Core;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.Sim;
using GarageGames.Torque.GameUtil;
using GarageGames.Torque.T2D;
using GarageGames.Torque.Platform;
using GarageGames.Torque.Materials;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Xyne.Components;
using Xyne.Templates;
using Microsoft.Xna.Framework.Input;
using Xyne.Regions;
using GarageGames.Torque.GFX;
using Xyne.Utilities;
using Xyne.Objects.Common;
using System.Collections;
using Xyne.Templates.PuzzlePieceTemplate;
using Xyne.GUI;
using GarageGames.Torque.GUI;
using Microsoft.Xna.Framework.GamerServices;
using Xyne.Network;
using Xyne.Events;

namespace Xyne
{
    public class Game : TorqueGame
    {
        // Object type IDs
        public const String PuzzlePieceName = "PuzzlePiece";
        public const String PuzzlePulseName = "PuzzlePulse";
        public const String PuzzleGlowName = "PuzzleGlow";
        public const String PlayerPiecesName = "PlayerPieces";
        public const String SweepName = "Sweep";
        public const String CardName = "Card";        
        public const String CharacterName = "Character";        
        // Board dimensions
        public const int BoardWidth = 25;
        public const int BoardHeight = 12;
        // Piece size
        public const int PuzzlePieceWidth = 40;
        public const int PuzzlePieceHeight = 40;
        // Object draw levels
        public const int BackgroundLayer = 100;
        public const int GrassLayer = 6;
        public const int PuzzlePieceLayer = 10;
        public const int PuzzleGlowLayer = 9;
        public const int PuzzlePulseLayer = 9;
        public const int CardLayer = 20;
        public const int CharacterLevel = 15;
        public const int EffectLevel = 10;
        public const int MenuItemLayer = 2;
        public const int SelectedMenuItemLayer = 1;
        public const int SweepLayer = 1;
        public const int BarsLayer = 2;
        // Velocities
        public const int FallingVelocity = 500;
        // Other constants
        public const int MaxCardLevel = 3;
        // Game instance
        private static Game game;
        // Network instance
        private static NetworkComponent network;
        // Client bounds
        private Vector2 clientsBounds = Vector2.Zero;

        public static Game Instance
        {
            get { return game; }
        }

        public Vector2 ClientBounds
        {
            get { return clientsBounds; }
        }

        public static NetworkComponent Network
        {
            get { return network; }
            set
            {
                if (network != null)
                {
                    game.Components.Remove(network);
                    network.CloseSession();
                }

                network = value;

                if (network != null)
                    game.Components.Add(network);
            }
        }

        public static void Main()
        {
            game = new Game();
            game.Run();
        }

        private Game()
        {
        }

        protected override void BeginRun()
        {
            base.BeginRun();

            EventManager.Instance = new EventManager(this);
            EventManager.Instance.Callbacks.Add(new EventManager.EventReceivedDelegate(EventReceived));
            Components.Add(EventManager.Instance);

            clientsBounds = new Vector2(Window.ClientBounds.Width, Window.ClientBounds.Height);

            Components.Add(new GamerServicesComponent(this));

            CanvasStateMachine.Instance.CreateGUI();
        }

        private void EventReceived(Event e)
        {
            if (e is GameQuitEvent)
            {
                //Network = null;
            }
        }        
    }
}